package com.et114.components.tree.tree51;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HtcTree {
    public static String buildTree(HtcTreeNode root, List childNodes, boolean hasRoot) {
        if (root == null || !hasRoot) {
            return buildChildNodesHtml(childNodes);
        }

        StringBuffer sb = new StringBuffer();
        if (root != null) {
            sb.append(HtcTreeNode.GYJ_TREE);
            sb.append(root.buildNodeContentHtml(hasRoot));
        }
        if (childNodes != null) {// && childNodes.size() > 0) {
            sb.append(">\n");
            sb.append(buildChildNodesHtml(childNodes));
        }
        if (root != null) {
            sb.append(HtcTreeNode.GYJ_TREE2);
        }
        return sb.toString();
    }

    public static String buildChildNodesHtml(List childNodes) {
        StringBuffer sb = new StringBuffer();
        for (Iterator iter = childNodes.iterator(); iter.hasNext();) {
            sb.append(buildNodeHtml((HtcTreeNode) iter.next()));
        }
        return sb.toString();
    }

    public static StringBuffer buildNodeHtml(HtcTreeNode node) {
        StringBuffer sb = new StringBuffer();
        sb.append(HtcTreeNode.GYJ_TREE);
        sb.append(node.buildNodeContentHtml());
        sb.append(" />\n");
        return sb;
    }

    public static List buildChildNodesList(List nodes) {
        List list = new ArrayList();
        for (Iterator iter = nodes.iterator(); iter.hasNext();) {
            list.add(buildNodeHtml((HtcTreeNode) iter.next()));
        }
        return list;
    }
}
